package com.example.blog2023.service.impl;

import com.example.blog2023.bean.pojo.Blog;
import com.example.blog2023.bean.vo.AdminBlogsVo;
import com.example.blog2023.bean.vo.GroupBlog;
import com.example.blog2023.bean.vo.LabelBlogVo;
import com.example.blog2023.bean.vo.LabelVo;
import com.example.blog2023.mapper.BlogLabelMapper;
import com.example.blog2023.mapper.BlogMapper;
import com.example.blog2023.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author cfy
 * @date 2023/12/16
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private BlogLabelMapper blogLabelMapper;
    @Override
    public ArrayList<GroupBlog> groupTypeList() {
        return blogMapper.groupBlog();
    }

    @Override
    public ArrayList<LabelVo> groupLabelList() {
        return blogLabelMapper.groupLabel();
    }

    @Override
    public ArrayList<AdminBlogsVo> getBlogByType(String type) {
        ArrayList<Blog> blogs = blogMapper.selectByType(type);
        return getAdminBlogsVo(blogs);
    }

    @Override
    public ArrayList<LabelBlogVo> getLabelByType(String label) {
        return blogLabelMapper.groupBlogByLabel(label);
    }

    @Override
    public Blog getBlogById(Integer id) {
        return blogMapper.selectById(id);
    }

    private ArrayList<AdminBlogsVo> getAdminBlogsVo(ArrayList<Blog> blogs){
        ArrayList<AdminBlogsVo> adminBlogsVos = new ArrayList<>();
        for(Blog b:blogs){
            char recommend = '否';
            if (b.getRecommend() == '1'){
                recommend = '是';
            }
            AdminBlogsVo adminBlogsVo = AdminBlogsVo.builder()
                    .id(b.getId())
                    .title(b.getTitle())
                    .createTime(b.getCreateTime())
                    .recommend(recommend)
                    .type(b.getType())
                    .build();
            adminBlogsVos.add(adminBlogsVo);
        }
        return adminBlogsVos;
    }
}
